package org.ims.inv.web.action.flow;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ims.inv.model.flow.ModelFlowBase;
import org.ims.inv.model.flow.ModelFlowTemplate;
import org.ims.inv.model.flow.material.ModelFlowMaterial;
import org.ims.inv.web.action.BaseAppAction;

import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.log.LogAnnotation;
import cn.trymore.core.util.UtilString;

/**
 * Action: 工作流基类
 * 
 * @author Jeccy.Zhao
 *
 */
public class FlowBaseAction
extends BaseAppAction
{
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(FlowBaseAction.class);
	
	public static final String NODE_NAME_TRIAL = "初审";
	
	public static final String NODE_KEY_TRIAL = "NODE_TRIAL";
	
	public static final String NODE_NAME_REVIEW = "复审";
	
	public static final String NODE_KEY_REVIEW = "NODE_REVIEW";
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 节点调位
	 */
	@LogAnnotation(description="数据节点(id-`${id}`, src-`${src}`)调位, (offset-`${offset}`)",type="流程设置")
	public ActionForward actionShiftFlowSequence (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			String paramId = request.getParameter("id");
			String paramOffset = request.getParameter("offset");
			String paramSource = request.getParameter("src");
			
			if (UtilString.isNotEmpty(paramOffset, paramId))
			{
				this.switchFlowSequence(paramId, paramSource, Integer.valueOf(paramOffset));
			}
		}
		catch (ServiceException e)
		{
			LOGGER.error("Exception raised when removing data", e);
			return ajaxPrint(response, getErrorCallback("数据删除失败,原因:" + e.getMessage()));
		}
		
		return null;
	}
	
	/**
	 * Switches flow sequence
	 * 
	 * @param entityId
	 * @param source
	 * @param offset
	 */
	private void switchFlowSequence (String entityId, String source, Integer offset) throws ServiceException
	{
		ModelFlowBase entity = null;
		if (ModelFlowBase.SRC_PROCESS_MATERIAL.equalsIgnoreCase(source))
		{
			entity = this.serviceWorkFlow.getServiceFlowMaterial().get(entityId);
		}
		else if (ModelFlowBase.SRC_PROCESS_TEMPLATE.equalsIgnoreCase(source))
		{
			entity = this.serviceWorkFlow.getServiceFlowTemplate().get(entityId);
		}
		else
		{
			throw new ServiceException("Unsupported source entity found..");
		}
		
		if (entity != null)
		{
			// ensures the new sequence number valid
			int totalSeq = this.serviceWorkFlow.getAvailableSeqs(entityId, source);
			int oldSeq = entity.getSequence();
			int newSeq = oldSeq + offset;
			if (newSeq < 1)
			{
				newSeq = 1;
			}
			else if (newSeq > totalSeq)
			{
				newSeq = totalSeq;
			}
			
			// switches sequence
			ModelFlowBase prevEntity = this.serviceWorkFlow.getBySequence(newSeq, source, entity);
			if (prevEntity != null)
			{
				prevEntity.setSequence(oldSeq);
				entity.setSequence(newSeq);
				
				if (ModelFlowBase.SRC_PROCESS_MATERIAL.equalsIgnoreCase(source))
				{
					this.serviceWorkFlow.getServiceFlowMaterial().save((ModelFlowMaterial)prevEntity);
					this.serviceWorkFlow.getServiceFlowMaterial().save((ModelFlowMaterial)entity);
				}
				else if (ModelFlowBase.SRC_PROCESS_TEMPLATE.equalsIgnoreCase(source))
				{
					this.serviceWorkFlow.getServiceFlowTemplate().save((ModelFlowTemplate)prevEntity);
					this.serviceWorkFlow.getServiceFlowTemplate().save((ModelFlowTemplate)entity);
				}
			}
			else
			{
				throw new ServiceException("The previous entity does not exist with sequence: " + newSeq);
			}
		}
		else
		{
			throw new ServiceException("Entity does not exist with id:" + entityId);
		}
	}
	
}
